package com.mz;

/**
 * Created by germmy on 17/9/26.
 */
import java.util.ArrayList;
import java.util.List;
import java.net.URI;

import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HeaderElementIterator;
import org.apache.http.HeaderIterator;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeaderElementIterator;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;


public class QuickStart {

    public static void main(String[] args) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            //GET方法
            HttpGet httpGet = new HttpGet("http://www.baidu.com");
            CloseableHttpResponse response1 = httpclient.execute(httpGet);
            response1.addHeader("Set-Cookie","c1=a; path=/; domain=localhost");
            response1.addHeader("Set-Cookie", "c2=b; path=\"/\", c3=c; domain=\"localhost\"");
            Header[] h1 = response1.getHeaders("Set-Cookie");
//          Header h2 = response1.getFirstHeader("Set-Cookie");
//          Header h3 = response1.getLastHeader("Set-Cookie");
//          Header[] h4 = response1.getAllHeaders();
//
            //通过URIBuilder来创建URI
            URI uri = new URIBuilder()
                    .setScheme("http")
                    .setHost("www.baidu.com")
                    .setPath("/search")
                    .setParameter("q", "httpclient")//搜索关键字
                    .setParameter("btnG", "Google Search")
                    .setParameter("aq", "f")
                    .setParameter("oq", "")
                    .build();//创建实例函数
            HttpGet httpGet2 = new HttpGet(uri);
            CloseableHttpResponse response2 = httpclient.execute(httpGet2);


            try {
                System.out.println("Http Get Request1:**********");
                System.out.println(httpGet.getURI());
                System.out.println("Http Get Response1:**********");
                System.out.println("Response1:                  "+response1.getStatusLine());
                System.out.println("Response1 ProtocolVersion:  "+response1.getProtocolVersion());
                System.out.println("Response1 StatusCode:       "+response1.getStatusLine().getStatusCode());
                System.out.println("Response1 ReasonPhrase:     "+response1.getStatusLine().getReasonPhrase());
                System.out.println("Response1status:            "+response1.getStatusLine().toString());
                System.out.println("Response1s head length:     "+h1.length);
//              System.out.println("Response1s head:            "+h2);
//              System.out.println("Response1s head:            "+h3);
//              System.out.println("Response1s head:            "+h4.toString());//?

                HeaderIterator it = response1.headerIterator("Set-Cookie");
                while (it.hasNext()) {
                    System.out.println(it.next());
                }

                HeaderElementIterator it2 = new BasicHeaderElementIterator(response1.headerIterator("Set-Cookie"));
                while (it2.hasNext()) {
                    HeaderElement elem = it2.nextElement();
                    System.out.println(elem.getName() + " = " + elem.getValue());
                    NameValuePair[] params = elem.getParameters();
                    for (int i = 0; i < params.length; i++) {
                        System.out.println(" " + params[i]);
                    }
                }

                HttpEntity entity1 = response1.getEntity();
                EntityUtils.consume(entity1);

                System.out.println("Http Get Request2:**********");
                System.out.println(httpGet2.getURI());
                System.out.println(httpGet2.toString());
                System.out.println("Http Get Response2:**********");
                System.out.println("Response2 status:           "+response2.getStatusLine());

                HttpEntity entity2 = response2.getEntity();
                EntityUtils.consume(entity2);
            } finally {
                response1.close();
                response2.close();
            }

            //POST方法
//            HttpPost httpPost = new HttpPost("http://*************/");
//            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
//            nvps.add(new BasicNameValuePair("***", "***"));
//            nvps.add(new BasicNameValuePair("***", "***"));
//            httpPost.setEntity(new UrlEncodedFormEntity(nvps));
//            CloseableHttpResponse response3 = httpclient.execute(httpPost);
//
//            try {
//                System.out.println("Http Post Request:**********");
//                System.out.println(httpPost.toString());
////              System.out.println(httpPost.);
//                System.out.println("Http Post Response:**********");
//                System.out.println(response3.getStatusLine());
//
//                HttpEntity entity2 = response3.getEntity();
//                EntityUtils.consume(entity2);
//            } finally {
//                response3.close();
//            }
        } finally {
            httpclient.close();
        }
    }

}
